<?php
	session_start();

	getAlbums();

	function getAlbums(){
		$userName = $_SESSION['userName'];

		$userDir = "../users/".$userName;

		// Create album list
		$response = array(
			"success" => false,
			"userName" => $userName,
			"albums" => array()
		);

		// Make sure requesting user exists
		if(is_dir($userDir)){
			// Make sure requested user directory exists
			if($handle = opendir($userDir)){
				// While there is another directory to look at, look at it
				while( false !== ($entry = readdir($handle))){
					$album = $userDir."/".$entry;
					// Removing folders we don't want the user to see
					// TODO There needs to be a better way to do this
					if( $entry != "." && $entry != ".." && $entry != ".svn"  && $entry != ".DS_Store"){
						// Add the directory name to the response object for javascript to use
						if($entry == "default"){
							array_unshift($response['albums'], $entry);
						}
						else{
							array_push($response['albums'], $entry);
						}
						$response["success"] = true;
					}
				}
			}
		}

		echo json_encode($response);
	}

?>
